package han.chensing.latos.common.level.util;

import org.luaj.vm2.LuaString;
import org.luaj.vm2.LuaTable;
import org.luaj.vm2.LuaValue;

public class LuaUtil {
    public static void printTable(LuaTable luaTable){
        printTable(luaTable,0);
    }

    public static void printTable(LuaTable luaTable,int tab){
        LuaValue[] keys = luaTable.keys();
        StringBuilder stringBuilder=new StringBuilder();
        stringBuilder.append("  ".repeat(Math.max(0, tab)));
        String tabs = stringBuilder.toString();
        for (LuaValue key : keys) {
            System.out.print(tabs);
            System.out.print(key.tojstring()+"=");
            LuaValue luaValue = luaTable.get(key);
            if (luaValue.istable()){
                LuaTable table = luaValue.checktable();
                if (table.keyCount()==0) {
                    System.out.println("Empty");
                    continue;
                }
                System.out.println();
                printTable(table,tab+1);
                continue;
            }
            boolean isString = luaValue instanceof LuaString;
            String jstring = luaValue.tojstring();
            System.out.println(isString?String.format("\"%s\"", jstring): jstring);
        }
    }
}
